﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Dora.Tools.Utility
{
    /// <summary>
    /// Key  Value  参数帮助类
    /// </summary>
  public  class ParameterHelper
    {
        //set时 自动排序  
        private SortedDictionary<string, Object> MDictionary = new SortedDictionary<string, object>();
        //private Hashtable MDictionary = new Hashtable();
        //设置
        public void SetValue(string key, object value)
        {
            //MDicNoSort[key] = value;
            MDictionary.Add(key, value);
        }
        //获取
        public object GetValue(string key)
        {
            object o = null;
            MDictionary.TryGetValue(key, out o);
            return o;
        }
        /// <summary>
        /// Url 参数化
        /// </summary>
        /// <returns></returns>
        public string ToUrl()
        {
            string buff = "";
            foreach (KeyValuePair<string, object> pair in MDictionary)
            {
                if (pair.Value == null)
                {
                    throw new Exception("内部含有值为null的字段!");
                }
                buff += pair.Key + "=" + pair.Value + "&";
            }
            buff = buff.Trim('&');
            return buff;
        }
        /// <summary>
        /// Url 参数化 BY 建行（加密参数） 
        /// </summary>
        /// <returns></returns>
        public string ToSignUrlByCCB()
        {
            string buff = "MERCHANTID="+MDictionary["MERCHANTID"] + "&POSID=" + MDictionary["POSID"] + "&BRANCHID=" + MDictionary["BRANCHID"] + "&ORDERID=" + MDictionary["ORDERID"] + "&PAYMENT=" + MDictionary["PAYMENT"] + "&CURCODE=" + MDictionary["CURCODE"]
                   + "&TXCODE=" + MDictionary["TXCODE"] + "&TYPE=" + MDictionary["TYPE"] + "&PUB=" + MDictionary["PUB"] + "&GATEWAY=" + MDictionary["GATEWAY"] + "&TRADE_TYPE=" + MDictionary["TRADE_TYPE"] + "&SUB_APPID=" + MDictionary["SUB_APPID"] + "&SUB_OPENID=" + MDictionary["SUB_OPENID"];
            return buff;
        }
        /// <summary>
        /// 直传参数
        /// </summary>
        /// <returns></returns>
        public string ToUrlByCCB()
        {
           string buff = "MERCHANTID=" + MDictionary["MERCHANTID"] + "&POSID=" + MDictionary["POSID"] + "&BRANCHID=" + MDictionary["BRANCHID"] + "&ORDERID=" + MDictionary["ORDERID"] + "&PAYMENT=" + MDictionary["PAYMENT"] + "&CURCODE=" + MDictionary["CURCODE"]
                   + "&TXCODE=" + MDictionary["TXCODE"] + "&TYPE=" + MDictionary["TYPE"] + "&GATEWAY=0"+"&TRADE_TYPE=" + MDictionary["TRADE_TYPE"] + "&SUB_APPID=" + MDictionary["SUB_APPID"] + "&SUB_OPENID=" + MDictionary["SUB_OPENID"];
            return buff;
        }
        /// <summary>
        /// 提供默认MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string MakeSign(string str)
        {
            var md5 = System.Security.Cryptography.MD5.Create();
            var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            var sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2"));
            }
            //所有字符转为大写
            return sb.ToString().ToUpper();
        }
    }
}
